package com.Maykk.Demo03;

import com.sun.jndi.cosnaming.IiopUrl;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @ClassName: SendInputTest
 * @Description: TODO UDP 发送键盘输入的内容，直到输入 over 结束
 * @Author: MaykkStar@163.com
 */
public class SendInputTest {
    public static void main(String[] args) throws IOException {
        // 实例化发送对象
        DatagramSocket ds = new DatagramSocket();

        // 创建要发送的数据
        String line;
        while (true) {
            System.out.print("请输入要发送的内容：");
            line = new Scanner(System.in).nextLine();

            // 判断是否退出
            if ("over".equals(line)) break;

            // 打包要发送的数据
            byte[] data = line.getBytes();
            int length = data.length;
            InetAddress address = InetAddress.getByName("Star");
            final int PORT = 9527;
            DatagramPacket dp = new DatagramPacket(data, length, address, PORT);

            // 发送
            ds.send(dp);
        }
        // 关闭发送
        ds.close();
    }
}
